package exttable;

import dbutils.DbUtils;
import exttable.threads.ThreadFactory;
import exttable.view.ViewPhases;
import exttable.view.ViewTestComboBox;
import utils.Localisation;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: May 11, 2010
 * Time: 4:46:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class StatisticPanel {
    private JComboBox cmbAnalysisList;
    private JComboBox cmbPhasesList;
    private JFrame statPanel;
    JButton createAnalysisWithTranslateFromDic;
    JButton createAnalysisGeneral;
    JButton createAnalysisWithColumnNameTranslate;

    public StatisticPanel() {
        statPanel = new JFrame();
//        statPanel.setLayout(new FlowLayout());

        try {
            cmbAnalysisList = new JComboBox(getCMBItems().toArray());
            cmbPhasesList = new JComboBox(getPhases().toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        createAnalysisWithTranslateFromDic = new JButton(Localisation.getMessage("form.downloadfile") + " " +
                Localisation.getMessage("withTranslateFromDic"));
        createAnalysisGeneral = new JButton(Localisation.getMessage("form.downloadfile"));
//        JLabel labelSelectAnalysis = new JLabel(Localisation.getMessage("form.selectanalys"));
//        JLabel labelStatReport = new JLabel(Localisation.getMessage("form.statreport"));
//        JSeparator jSeparator1 = new JSeparator();

        createAnalysisWithColumnNameTranslate = new JButton(Localisation.getMessage("form.downloadfile") + " " +
                Localisation.getMessage("withColumnNameTranslate"));

        createAnalysisWithTranslateFromDic.addActionListener(new ActionListener() {
            ViewTestComboBox vcb;
            ViewPhases vp;
            File f;

            @Override
            public void actionPerformed(ActionEvent e) {
                vcb = (ViewTestComboBox) cmbAnalysisList.getSelectedItem();
                vp = (ViewPhases) cmbPhasesList.getSelectedItem();
//                System.out.println("" + vp.getPhaseId());
                final JFileChooser fc = new JFileChooser();
                fc.setDialogTitle(Localisation.getMessage("form.selectplaceandname"));
                fc.showSaveDialog(statPanel);
                f = fc.getSelectedFile();
                Thread th=new Thread(ThreadFactory.runStatReport(vcb, f.getAbsolutePath(), vp.getPhaseId(), true, false));
                if (f.getAbsolutePath() != null) {
                    th.start();
                }
//                try {
//                    StatReportAnalysis.formXLSReport(vcb, f.getAbsolutePath(), vp.getPhaseId(), true,false);
//                } catch (Exception e1) {
//                    e1.printStackTrace();
//                }
            }
        });
        createAnalysisGeneral.addActionListener(new ActionListener() {
            ViewTestComboBox vcb;
            ViewPhases vp;
            File f;

            @Override
            public void actionPerformed(ActionEvent e) {
                vcb = (ViewTestComboBox) cmbAnalysisList.getSelectedItem();
                vp = (ViewPhases) cmbPhasesList.getSelectedItem();
//                System.out.println("" + vp.getPhaseId());
                final JFileChooser fc = new JFileChooser();
                fc.setDialogTitle(Localisation.getMessage("form.selectplaceandname"));
                fc.showSaveDialog(statPanel);
                f = fc.getSelectedFile();
                Thread th= new Thread(ThreadFactory.runStatReport(vcb, f.getAbsolutePath(), vp.getPhaseId(), false, false));
                if (f.getAbsolutePath() != null) {
                    th.start();
                }
//                try {
//                    StatReportAnalysis.formXLSReport(vcb, f.getAbsolutePath(), vp.getPhaseId(), false, false);
//                } catch (Exception e1) {
//                    e1.printStackTrace();
//                }
            }
        });

        createAnalysisWithColumnNameTranslate.addActionListener(new ActionListener() {
            ViewTestComboBox vcb;
            ViewPhases vp;
            File f;

            @Override
            public void actionPerformed(ActionEvent e) {
                vcb = (ViewTestComboBox) cmbAnalysisList.getSelectedItem();
                vp = (ViewPhases) cmbPhasesList.getSelectedItem();
//                System.out.println("" + vp.getPhaseId());
                final JFileChooser fc = new JFileChooser();
                fc.setDialogTitle(Localisation.getMessage("form.selectplaceandname"));
                fc.showSaveDialog(statPanel);
                f = fc.getSelectedFile();
                Thread th= new Thread(ThreadFactory.runStatReport(vcb, f.getAbsolutePath(), vp.getPhaseId(), true, true));
                if (f.getAbsolutePath() != null) {
                    th.start();
                }
//                try {
//                    StatReportAnalysis.formXLSReport(vcb, f.getAbsolutePath(), vp.getPhaseId(), true, true);
//                } catch (Exception e1) {
//                    e1.printStackTrace();
//                }
            }
        });

        statPanel.add(cmbPhasesList);
        statPanel.add(cmbAnalysisList);
        statPanel.add(createAnalysisGeneral);
        statPanel.add(createAnalysisWithTranslateFromDic);
        statPanel.add(createAnalysisWithColumnNameTranslate);
        organiseLayout();
        statPanel.setSize(500, 250);
        statPanel.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        statPanel.setTitle(Localisation.getMessage("form.downloadfile"));
        statPanel.setVisible(true);
    }


    private List<ViewTestComboBox> getCMBItems() throws Exception {
        Connection s;
        try {
            s = DbUtils.getDBConnectionGeneral();

            PreparedStatement ps = s.prepareStatement("SELECT * FROM dic_analysis_instr_invest_list");
            ResultSet rs2 = ps.executeQuery();
            List<ViewTestComboBox> list = new ArrayList<ViewTestComboBox>();
            while (rs2.next()) {
                ViewTestComboBox ctc = new ViewTestComboBox.Build()
                        .id(rs2.getString(1))
                        .analysisName(rs2.getString("analysis_name"))
                        .nameRu(rs2.getString("name_ru"))
                        .nameEn(rs2.getString("name_ru"))
                        .nameKz(rs2.getString("name_kk"))
                        .build();
                list.add(ctc);
            }
            s.close();
            return list;
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new Exception("db eror");
        }
    }

    private List<ViewPhases> getPhases() throws Exception {
        Connection s;
        try {
            s = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = s.prepareStatement("SELECT * FROM dic_phase ORDER BY name_en");
            ResultSet rs2 = ps.executeQuery();
            List<ViewPhases> list = new ArrayList<ViewPhases>();
            while (rs2.next()) {
                ViewPhases phase = new ViewPhases();
                phase.setPhaseId(rs2.getString(1));
                phase.setNameEn(rs2.getString("name_ru"));
                phase.setNameKz(rs2.getString("name_kk"));
                phase.setNameRu(rs2.getString("name_ru"));
                list.add(phase);
            }
            s.close();
            return list;


        }
        catch (Exception e) {
            e.printStackTrace();
            throw new Exception("db eror");
        }

    }


    private void organiseLayout() {
        SpringLayout layout = new SpringLayout();
        statPanel.setLayout(layout);
        layout.putConstraint(SpringLayout.WEST, cmbPhasesList, 15, SpringLayout.WEST, statPanel);
        layout.putConstraint(SpringLayout.NORTH, cmbPhasesList, 5, SpringLayout.NORTH, statPanel);

        layout.putConstraint(SpringLayout.WEST, cmbAnalysisList, 15, SpringLayout.WEST, statPanel);
        layout.putConstraint(SpringLayout.NORTH, cmbAnalysisList, 10, SpringLayout.SOUTH, cmbPhasesList);

        layout.putConstraint(SpringLayout.WEST, createAnalysisGeneral, 15, SpringLayout.WEST, statPanel);
        layout.putConstraint(SpringLayout.NORTH, createAnalysisGeneral, 10, SpringLayout.SOUTH, cmbAnalysisList);

        layout.putConstraint(SpringLayout.WEST, createAnalysisWithTranslateFromDic, 15, SpringLayout.WEST, statPanel);
        layout.putConstraint(SpringLayout.NORTH, createAnalysisWithTranslateFromDic, 40, SpringLayout.SOUTH, cmbAnalysisList);

        layout.putConstraint(SpringLayout.WEST, createAnalysisWithColumnNameTranslate, 15, SpringLayout.WEST, statPanel);
        layout.putConstraint(SpringLayout.NORTH, createAnalysisWithColumnNameTranslate, 70, SpringLayout.SOUTH, cmbAnalysisList);
    }
}
